package com.motu.monstercity.server.game.log;

import com.motu.monstercity.server.game.base.AllParam;
import com.motu.monstercity.server.game.userdata.UserInfo;
import com.motu.vertx.module.utility.kafka.KafkaLogManager;
import com.motu.vertx.module.utility.threadpool.DBLogManager;
import com.motu.vertx.module.utility.toolset.TimeUtils;
import com.motu.vertx.module.utility.toolset.Tool;
import io.vertx.core.json.JsonArray;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 游戏日志管理类
 */
public class MotuLogManager extends DBLogManager {
	private final static Logger logger = LoggerFactory.getLogger(MotuLogManager.class);

	/**
	 * 记录物品获得日志
	 */
	public static void logItemIncome(UserInfo userInfo, int itemId, long dbid, long balance, long income, Object... params) {
		String tableName = getTableName();// 固定用表record_item

		int type = Integer.parseInt(String.valueOf(params[0]));// 日志类型
		int itemLogType = type;//用做数数SDK日志类型
		long amountBefore = balance;//获取变化前的道具数量
		long amountAfter = amountBefore + income;//获取变化后的道具数量

		//数数SDK控制发送的物品类型
		if (isInItemArray(itemId) || isInItemRange(itemId)){
			//数数SDK道具获取事件
			ThinkingDataManager.pushItemEvent(userInfo ,ThinkingDataManager.EVENT_ITEM_GET,itemId,itemLogType,amountBefore,income,amountAfter,getExtParam(params));
		}


		if (!addItemRecord(tableName, userInfo.getId(), userInfo.getServerId(), userInfo.getLevel(), userInfo.getVip(), type, itemId, dbid, balance, 0, income, params)) {
			String paraminfo = "";
			for (int i = 1; i < params.length; i++) {
				if (i == params.length - 1) {  //是最后一个
					paraminfo += params[i];
				} else {
					paraminfo += params[i] + ",";
				}
			}
			logger.error("addItemRecord error userId = " + userInfo.getId() + " type = " + type + " itemid = " + itemId + " dbid = " + dbid + " balance = " + balance + " income = " + income + " paraminfo = " + paraminfo);
		}
	}

	/**
	 * 记录物品消耗日志
	 */
	public static void logItemConsume(UserInfo userInfo, int itemId, long dbid, long balance, long consume, Object... params) {
		String tableName = getTableName();

		int type = Integer.parseInt(String.valueOf(params[0]));// 日志类型
		int itemLogType = type;//用做数数SDK日志类型
		long amountBefore = balance;//获取变化前的道具数量
		long amountAfter = amountBefore - consume;//获取变化后的道具数量

		//数数SDK控制发送的物品类型
		if (isInItemArray(itemId) || isInItemRange(itemId)){
			//数数SDK道具获取事件
			ThinkingDataManager.pushItemEvent(userInfo,ThinkingDataManager.EVENT_ITEM_COST,itemId,itemLogType,amountBefore,consume,amountAfter,getExtParam(params));
		}


		if (!addItemRecord(tableName, userInfo.getId(), userInfo.getServerId(), userInfo.getLevel(), userInfo.getVip(), type, itemId, dbid, balance, consume, 0, params)) {
			String paraminfo = "";
			for (int i = 1; i < params.length; i++) {
				if (i == params.length - 1) {  //是最后一个
					paraminfo += params[i];
				} else {
					paraminfo += params[i] + ",";
				}
			}
			logger.error("addItemRecord error userId = " + userInfo.getId() + " type = " + type + " itemid = " + itemId + " dbid = " + dbid + " balance = " + balance + " consume = " + consume + " paraminfo = " + paraminfo);
		}
	}

	/**
	 * 消耗获得日志写入数据库
	 */
	private static boolean addItemRecord(String tableName, long userId, int serverId, int level, int vip, int type, int itemId, long dbid, long balance, long consume, long income, Object... params) {
		String paraminfo = getExtParam(params);
		long curTime = System.currentTimeMillis();
		String sql = "insert into  " + tableName + "(userid,serverid,level,vip,type,itemid,dbid,balance,consume,income,time,param,atime) values(?,?,?,?,?,?,?,?,?,?,?,?,?);";
		JsonArray info = new JsonArray();
		info.add(userId);
		info.add(serverId);
		info.add(level);
		info.add(vip);
		info.add(type);
		info.add(itemId);
		info.add(dbid);
		info.add(balance);
		info.add(consume);
		info.add(income);
		info.add(curTime);
		info.add(paraminfo);
		info.add(TimeUtils.getMsTimeFullStr(curTime));
		KafkaLogManager.addItemRecordLog(info);
		return true;
	}

	/**
	 * 消耗获得日志表名
	 */
	public static String getTableName() {
		return "record_item";
	}

	/**
	 * 获取额外参数
	 *
	 * @param params
	 * @return
	 */
	public static String getExtParam(Object... params) {
		String paraminfo = "";
		for (int i = 1; i < params.length; i++) {
			if (i == params.length - 1) {  //是最后一个
				paraminfo += params[i];
			} else {
				paraminfo += params[i] + ",";
			}
		}
		return paraminfo;
	}


	/**
	 * 判断物品ID是否在配置的物品列表中
	 */
	private static boolean isInItemArray(int itemId){
		if (Tool.isEmpty(AllParam.THINKINGDATA_ITEM_TYPE)){
			return false;
		}
		JsonArray itemArray = new JsonArray(AllParam.THINKINGDATA_ITEM_TYPE);
		return Tool.isInList(itemArray, itemId);
	}

	/**
	 * 判断物品ID是否在配置的物品范围内
	 */
	private static boolean isInItemRange(int itemId){
		if (Tool.isEmpty(AllParam.THINKINGDATA_ITEM_RANGE)){
			return false;
		}
		JsonArray rangeArray = new JsonArray(AllParam.THINKINGDATA_ITEM_RANGE);
		for (int i = 0; i < rangeArray.size(); i++) {
			JsonArray range = rangeArray.getJsonArray(i);
			int startId = range.getInteger(0);
			int endId = range.getInteger(1);
			if (itemId >= startId && itemId <= endId){
				return true;
			}
		}
		return false;
	}

}
